package com.yuanduan.mplus.util;

import android.util.Log;

import com.cj.framework.util.RSAUtils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;

/**
 * Created by cj on 2016/6/28.
 * Email:781806481@qq.com
 */
public class FileImageUpload {
    private static final int TIME_OUT=10*1000000;
    private static final String CHARSET="utf-8";
    public  static final String SUCESS="1";
    public static final String FAIL="0";

    public static void  uploadFile(File file, String fileParam, HashMap<String,String> map,String requestURL,ImageCallback callback){
        String BOUNDARY= UUID.randomUUID().toString();
        String LINE_END="\r\n";
        String PREFIX="--";
        String CONTENT_TYPE="multipart/form-data";
        try{
            URL url=new URL(requestURL);
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setReadTimeout(TIME_OUT);
            connection.setUseCaches(false);
            connection.setConnectTimeout(TIME_OUT);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("charset",CHARSET);
            connection.setRequestProperty("connection","keep-alive");
            connection.setRequestProperty("content-type",CONTENT_TYPE+";boundary="+BOUNDARY);
            if(file==null){
                callback.onFail();
                return;
            }

            //写入数据体文件部分
            OutputStream os=connection.getOutputStream();
            DataOutputStream dos=new DataOutputStream(os);
            StringBuffer sb=new StringBuffer();
            sb.append(PREFIX);
            sb.append(BOUNDARY);
            sb.append(LINE_END);
            sb.append("Content-Disposition:form-data;name=\""+fileParam+"\";filename=\""+file.getName()+"\""+LINE_END);
            sb.append("Content-Type:"+"image/jpeg"+";charset="+CHARSET+LINE_END);
            sb.append(LINE_END);
            dos.write(sb.toString().getBytes());
            InputStream is=new FileInputStream(file);
            byte[] buffer=new byte[1024];
            int len=0;
            while((len=is.read(buffer))!=-1){
                dos.write(buffer,0,len);
            }
            is.close();

            dos.write(LINE_END.getBytes());
            String pp=PREFIX+BOUNDARY+LINE_END;
            Iterator iterator=map.keySet().iterator();
            String key;
            String value;
            while(iterator.hasNext()){
                key= (String) iterator.next();
                value=map.get(key);
                dos.write(pp.getBytes());
                String str="Content-Disposition:form-data;name=\""+key+"\""+LINE_END+LINE_END+value+LINE_END;
                dos.write(str.getBytes());
            }
            dos.write((PREFIX+BOUNDARY+PREFIX).getBytes());
            dos.flush();

            int responseCode=connection.getResponseCode();
            InputStream inputStream=connection.getInputStream();
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            byte[] buff=new byte[1024];
            int ll=0;
            while((ll=inputStream.read(buff))!=-1){
                bos.write(buff,0,ll);
            }
            Log.d("xx","response rsa"+ RSAUtils.decryptWithRSA(bos.toString()));
            callback.onSuccess();
            return;
    } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        callback.onFail();
        return;
    }

    public interface ImageCallback{
        public void onFail();
        public void onSuccess();
    }

}
